'''
Created on May 12, 2012

@author: nicole
'''

import pickle
from copy import *

#test


def read(filepath):
    "reads the kaggle_visible_evaluation_triplets.txt file (name still has to be in the filepath) and generates a dictionary of a dictionary of it"
    currentuserid= ""
    songs_heard = []
    userid_songs = []
    
    '''
    with open('../kaggle_users.txt', 'r') as file:
        canonical_users = map(lambda line: line.strip(), file.readlines())
        pass
    '''
    f = open(filepath, 'r')
    
    
    for line in f:
        userid, song, heard = line.strip().split('\t')
        #userid = int(canonical_users.index(userString))
        
        if currentuserid == "":
            currentuserid = userid
            
        if userid == currentuserid:
            songs_heard.append(song) 
        else:
            #save the current user and its dict to the big dict, and reset the values
            #deepcopy
            #print userindex
            userid_songs.append(songs_heard)
            #print songs_heard
            currentuserid = userid
            songs_heard = []
            songs_heard.append(song)
    
    userid_songs.append(songs_heard)
    
    f.close()
    #print userid_songs
    return userid_songs

def readallid(filepath):
    currentuserid= ""
    songs_heard = []
    userid_songs = []
   
    filesongs = open('../data/kaggle_songs.txt', 'r')
    song_to_index = dict(map(lambda line:line.strip().split(' '),filesongs.readlines()))
    filesongs.close()

    
    f = open(filepath, 'r')
        
    for line in f:
        userid, song, heard = line.strip().split('\t')
        
        songid = song_to_index[song]
        
        if currentuserid == "":
            currentuserid = userid
            
        if userid == currentuserid:
            songs_heard.append(songid) 
        else:
            userid_songs.append(songs_heard)
            currentuserid = userid
            songs_heard = []
            songs_heard.append(songid)
    
    userid_songs.append(songs_heard)
    
    f.close()
    return userid_songs    

def readallidforcluster(filepath):
    currentuserid= ""
    songs_heard = []
    userid_songs = []
   
    filesongs = open('../data/kaggle_songs.txt', 'r')
    song_to_index = dict(map(lambda line:line.strip().split(' '),filesongs.readlines()))
    filesongs.close()

    
    f = open(filepath, 'r')
        
    for line in f:
        userid, song, heard = line.strip().split('\t')
        
        songid = int(song_to_index[song])
        heard = int (heard)
        if currentuserid == "":
            currentuserid = userid
            
        if userid == currentuserid:
            songs_heard.append((songid, heard)) 
        else:
            userid_songs.append(songs_heard)
            currentuserid = userid
            songs_heard = []
            songs_heard.append((songid, heard))
    
    userid_songs.append(songs_heard)
    
    f.close()
    return userid_songs

def writeobject(filepath, myobject):
    # write python dict to a file
    output = open(filepath, 'wb')
    pickle.dump(myobject, output)
    output.close()

def readdict(filepath):
# read python dict back from the file
    pkl_file = open(filepath, 'rb')
    mydict = pickle.load(pkl_file)
    pkl_file.close()
    return mydict
